﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Esa
{
    using AAB;
    [RequireComponent(typeof(AudioSource))]
    [RequireComponent(typeof(BypassAudioSource))]
    public class SourceWrapper : MonoBehaviour
    {
        public void Reset()
        {
            var _as = GetComponent<AudioSource>();
            _as.playOnAwake = false;
        }
        public BypassAudioSource audioSourceB;
        AudioSource audioSource;
        public float tFade; // 淡出进度
        public float fadeDuration; // 淡出需要时间（秒）
        public AnimationCurve fadeCurve;
        bool onPC;
        public bool hold; // 延音
        private void Awake()
        {
            onPC = Application.platform != RuntimePlatform.Android;
            audioSourceB = GetComponent<BypassAudioSource>();
            audioSource = GetComponent<AudioSource>();
        }
        private void Update()
        {
            if (hold || tFade > 1.2) return;

            var delta = Time.deltaTime / fadeDuration;
            tFade += delta;
            if (onPC)
                audioSource.volume = fadeCurve.Evaluate(tFade);
        }
        [Button]
        public void Play()
        {
            tFade = 0;
            if (onPC)
            {
                audioSource.Play();
            }
            else audioSourceB.Play();
        }
    }
}